﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NangLuongTieuDiemDAO.DAO;
using NangLuongTieuDiemDTO.DTO;

namespace NangLuongTieuDiemBUS.BUS
{
    public class ChiTietSanPhamHoaDonBUS
    {
        #region "[Insert]"
               
        public int Insert( ChiTietSanPhamHoaDonDTO ctspDto)
        {
             // Kiem tra Business Rule
            // Them
            int IDResult = -1;
            try
            {
                IDResult = ChiTietSanPhamHoaDonDAO.Insert(ctspDto);
            }
            catch(Exception ex) 
            {
                InSert_LoiHeThong("Insert", ex);
            }
            return IDResult;
            
        }
        #endregion

        #region "[Delete]"
            public bool Delete(int ID)
            {
                 // Kiem tra Business Rule



                // Xoa
                bool IDResult = false;
                try
                {
                    IDResult = ChiTietSanPhamHoaDonDAO.Delete(ID);
                }
                catch (Exception ex)
                {
                    InSert_LoiHeThong("Delete", ex);
                }
                return IDResult;
            }
       
        #endregion

        #region "[Update]"
            public bool Update(ChiTietSanPhamHoaDonDTO ctspDto)
            {
                 // Kiem tra Business Rule



                // Sua
                bool IDResult = false;
                try
                {
                    IDResult = ChiTietSanPhamHoaDonDAO.Update(ctspDto);
                }
                catch (Exception ex)
                {
                    InSert_LoiHeThong("Update", ex);
                }
                return IDResult;
            }
        #endregion

        #region "[Get all]"
            public List<ChiTietSanPhamHoaDonDTO> GetAll()
            {
                List<ChiTietSanPhamHoaDonDTO> dsctspDto = new List<ChiTietSanPhamHoaDonDTO>();
                try
                {
                    dsctspDto = ChiTietSanPhamHoaDonDAO.GetAll();
                }
                catch (Exception ex)
                {
                    InSert_LoiHeThong("GetAll", ex);
                }
                return dsctspDto;
            }
        #endregion

        #region "[SelectByPrimaryKey]"
            public ChiTietSanPhamHoaDonDTO SelectByPrimaryKey(int ID)
            {
                ChiTietSanPhamHoaDonDTO ctspDto = new ChiTietSanPhamHoaDonDTO();
                try
                {
                    ctspDto = ChiTietSanPhamHoaDonDAO.SelectByPrimaryKey(ID);
                }
                catch (Exception ex)
                {
                    InSert_LoiHeThong("SelectByPrimaryKey", ex);
                }
                return ctspDto;
            }
        #endregion

        #region "[Hàm insert lỗi hệ thống]"
            public void InSert_LoiHeThong(string TenHam, Exception ex)
            {
                LoiHeThongDTO lhtDto = new LoiHeThongDTO();
                lhtDto.TenLoi = ex.Message.ToString();
                lhtDto.NgayTao = DateTime.Now;
                lhtDto.TenHamBiLoi = "ChiTietSanPhamHoaDonBUS_" + TenHam;
                lhtDto.SuaLoi = false;
                lhtDto.GhiChu = "";
                LoiHeThongDAO.Insert(lhtDto);
               
            }
       #endregion
    }
}
